<template>
  <div class="app-container">
    <el-row :gutter="20">
        <!-- 系统树数据-->
        <el-col :span="4" :xs="24">
          <DictTreeLeft  dict="wms_process_state"
            @ok="treeNodeClick" />
        </el-col>

    <el-col :span="20" :xs="24">
    <el-form :model="pd.queryParams" ref="queryRef" :inline="true" v-show="pd.showSearch" label-width="68px">
      <el-form-item label="工序名称" prop="name">
        <el-input
          v-model="pd.queryParams.name"
          placeholder="请输入工序名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['wms:process:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="pd.single"
          @click="handleUpdate"
          v-hasPermi="['wms:process:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="pd.multiple"
          @click="handleDelete"
          v-hasPermi="['wms:process:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['wms:process:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="pd.showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="pd.loading" 
    :data="pd.processList" 
    @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="工序名称" align="center" prop="name" />
      <!-- <el-table-column label="工艺类别" align="center" prop="treeId" /> -->
      <el-table-column label="工艺要求" align="center" prop="attention" />
      <el-table-column label="是否启用" align="center" prop="enableFlag">
        <template #default="scope">
          <dict-tag :options="sys_is" :value="scope.row.enableFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建者" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新者" align="center" prop="updateBy" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" width="180" label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['wms:process:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['wms:process:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="pd.total>0"
      :total="pd.total"
      v-model:page="pd.queryParams.pageNum"
      v-model:limit="pd.queryParams.pageSize"
      @pagination="getList"
    />
    </el-col>
  </el-row>
    <ProcessCurd ref="processCurdRef" @ok="processCurdOk"></ProcessCurd>
  </div>
</template>

<script setup name="Process">
import ProcessCurd from "@/views/wms/process/curd.vue"
import { processGetPage, getProcess } from "@/api/wms/process";
import {getCurrentInstance, reactive} from "vue";
import { useRoute } from "vue-router";
import DictTreeLeft from "@/views/sys/dict/treeLeft";

const { proxy } = getCurrentInstance();
const {sys_is} = proxy.useDict('sys_is');
const route = useRoute();

onMounted(() => initPage());

const pd = reactive({
  processList: [],
  open: false,
  total: 0,
  loading: true,
  showSearch: true,
  ids: [],
  form: {},
  single: true,
  multiple: true,
  operation: 0,
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    treeId: null,
  },
  rules: {
    name: [
      { required: true, message: "工序名称不能为空", trigger: "blur" }
    ],
    treeId: [
      { required: true, message: "工艺类别不能为空", trigger: "change" }
    ],
    enableFlag: [
      { required: true, message: "是否启用不能为空", trigger: "blur" }
    ],
  }
});

/** 查询工序列表 */
function getList() {
  pd.loading = true;
  debugger
  processGetPage(pd.queryParams).then(response => {
    pd.processList = response.rows
    pd.total = response.total;
    pd.loading = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  pd.queryParams.pageNum = 1;
  getList();
}

// 取消按钮
function cancel() {
  pd.open = false;
  reset();
}
/** 重置按钮操作 */
function resetQuery() {
  pd.queryParams.name = null
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  pd.ids = selection.map(item => item.id);
  pd.single = selection.length != 1;
  pd.multiple = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
 proxy.$refs["processCurdRef"].add();
}

/** 修改按钮操作 */
function handleUpdate(row) {
  const _id = row.id || pd.ids
  getProcess(_id).then(response => {
    debugger
    proxy.$refs["processCurdRef"].update(response.data);
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || pd.ids;
  proxy.$refs["processCurdRef"].del(_ids);
}

/** 对话框完成后刷新界面 */
function processCurdOk(treeId){
  pd.queryParams.treeId = treeId;
  getList();
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('wms/process/export', {
    ...pd.queryParams
  }, `process_${new Date().getTime()}.xlsx`)
}

function treeNodeClick(tree) {
  pd.queryParams.treeId = tree.id;
  getList();
}

function initPage() {
  pd.queryParams = { ...pd.queryParams, ...route.query };
  getList();
}

getList();
</script>
